//
//  RadarOneWayCell.m
//  carcareIOS
//
//  Created by 王然 on 16/6/29.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "RadarOneWayCell.h"
#define TOP_HEIGHT (15)
#define RADARV_HEIGHT (400 * __MAIN_RATIO_375)
#define FOOTER_SMALLV_TOP (12)
#define FOOTER_SMALLV_HEIGHT (83 * __MAIN_RATIO_375)
#define FOOTER_VIEW_HEIGHT ((FOOTER_SMALLV_TOP + FOOTER_SMALLV_HEIGHT) * 2)

@interface RadarOneWayCell()

@property (nonatomic , strong) UIView * footerHeadV;

@property (nonatomic , strong) UIView * footerBottomV;

@property (nonatomic , strong) RadarPictureView * radarView;


@end

@implementation RadarOneWayCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView
{
    self.radarView = [[RadarPictureView alloc]initWithFrame:CGRectMake(0, TOP_HEIGHT, __MAIN_WIDTH, RADARV_HEIGHT)];
    self.radarView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.radarView];
    
    //底部view
    UIView * footerView = [[[[UIView viewWithFrame:CGRectMake(0, self.radarView.bottom, __MAIN_WIDTH,FOOTER_VIEW_HEIGHT)] resetBackgroundColor:[UIColor whiteColor]] setupOnView:self] resetBorderWidth:0.5 borderColor:[Theme lineColor]];
    footerView.backgroundColor = [UIColor clearColor];

    self.footerHeadV = [[[UIView viewWithFrame:CGRectMake(0, FOOTER_SMALLV_TOP, __MAIN_WIDTH,FOOTER_SMALLV_HEIGHT)] resetBackgroundColor:[UIColor clearColor]] setupOnView:footerView];
    self.footerBottomV = [[[UIView viewWithFrame:CGRectMake(0, self.footerHeadV.bottom, __MAIN_WIDTH,FOOTER_SMALLV_HEIGHT)] resetBackgroundColor:[UIColor clearColor]] setupOnView:footerView];
    
}
- (void)setDriveModel:(OneDriveModel *)driveModel
{
    _driveModel = driveModel;
    if ([driveModel modelDic].count == 0 ) {
        return;
    }
    self.radarView.totalPointsStr = STRING_INT(driveModel.mark);
    NSArray * numArray = @[@(driveModel.speedControl),@(driveModel.addSpeedControl),@(driveModel.lowSpeedControl),@(driveModel.driveCarLongControl)];
    self.radarView.topTimeCountInt = driveModel.haTimes;
    self.radarView.rightTimesCountInt = driveModel.rdTimes;
    self.radarView.funcArray = numArray;
    
    [self reloadForeHeadArr:@[@[[NSString stringWithFormat:@"%.1f",driveModel.mile],@"里程(km)"],@[STRING_INT(driveModel.travelTime),@"耗时(min)"],@[[NSString stringWithFormat:@"%.1f",driveModel.maxSpeed],@"最高(km/h)"],@[[NSString stringWithFormat:@"%.1f",driveModel.avgSpeed],@"均速(km/h)"]] withThreeBottomArr:@[@[[NSString stringWithFormat:@"%.02f",driveModel.fuelConsum],@"耗油(升)"],@[[NSString stringWithFormat:@"%.1f",driveModel.fuelCost],@"油费(元)"],@[[NSString stringWithFormat:@"%.02f",driveModel.hundredFuel],@"百公里(升)"]]];
}
//设置雷达图下边的 里程  耗时 最高 等等数据
- (void)reloadForeHeadArr:(NSArray *)headArr withThreeBottomArr:(NSArray *)bottomArr
{
    [self.footerHeadV removeAllSubviews];
    [self.footerBottomV removeAllSubviews];
    
    CGFloat viewH = 53 * __MAIN_RATIO_375;
    CGFloat viewAndViewX = 5;
    CGFloat leftX = 22 * __MAIN_RATIO_375;
    CGFloat centerSmallViewY = (self.footerBottomV.height - viewH) / 2;
    CGFloat headViewW = (__MAIN_WIDTH - leftX * 2 - viewAndViewX * (headArr.count - 1)) / headArr.count;
    for (int index = 0 ; index < headArr.count; index ++) {
        CGFloat viewX = leftX + (headViewW + viewAndViewX) * index;
        [self viewWithFrame:CGRectMake(viewX, centerSmallViewY, headViewW, viewH) withSuperView:self.footerHeadV withTitle:[headArr[index] firstObject] withTitleFont:FONT_DEFAULT_Light(25 * __MAIN_RATIO_375)  withDetailTitle:[headArr[index] lastObject] withAlign:NSTextAlignmentCenter];
    }
    
    
    CGFloat centerSmallViewW = (__MAIN_WIDTH - viewAndViewX * (bottomArr.count - 1)) / bottomArr.count;
    for (int index = 0; index < bottomArr.count; index ++) {
        CGFloat viewX = (centerSmallViewW + viewAndViewX) * index;
        [self viewWithFrame:CGRectMake(viewX, centerSmallViewY, centerSmallViewW, viewH) withSuperView:self.footerBottomV withTitle:[bottomArr[index] firstObject] withTitleFont:FONT_DEFAULT_Light(23 * __MAIN_RATIO_375)  withDetailTitle:[bottomArr[index] lastObject] withAlign:(index == 0 ? NSTextAlignmentCenter : (index == bottomArr.count - 1 ? NSTextAlignmentCenter : NSTextAlignmentCenter ))];
    }
}
#pragma mark - 创建具有主标题 副标题的每个小view
- (void)viewWithFrame:(CGRect)frame withSuperView:(UIView *)superView withTitle:(NSString *)title withTitleFont:(UIFont *)titleFont withDetailTitle:(NSString *)detailTitle withAlign:(NSTextAlignment)textAlignment
{
    UIView * backView = [[[UIView viewWithFrame:frame] resetBackgroundColor:[UIColor redColor]] setupOnView:superView];
    backView.backgroundColor = [UIColor clearColor];
    CGFloat titleLabelH = 23;
    CGFloat detailLabelH = 20;
    [Factory labelWithFrame:CGRectMake(0, 0, backView.width, titleLabelH) font:titleFont text:title textColor:[Theme wordColor] onView:backView textAlignment:NSTextAlignmentCenter];
    CGFloat lableAndLabelY = backView.height - titleLabelH - detailLabelH;
    [Factory labelWithFrame:CGRectMake(0, titleLabelH + lableAndLabelY, backView.width, detailLabelH) font:FONT_DEFAULT_Light(13 * __MAIN_RATIO_375) text:detailTitle textColor:[Theme wordGrayNewColor] onView:backView textAlignment:textAlignment];
    
}

+ (CGFloat)getCellHeight
{
    return TOP_HEIGHT + RADARV_HEIGHT + FOOTER_VIEW_HEIGHT ;
}
@end
